package com.xmb.wechat.view.alipay;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.constraint.Group;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.util.WechatDateFormat;
import com.xmb.wechat.widget.NoPaddingTextView;
import com.xmb.wechat.widget.rc.RCConstraintLayout;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class AliPayRedPacketDetailActivity extends BaseActivity {

    //通用
    @BindView(R2.id.iv_avatar)
    ImageView mIvAvatar;
    @BindView(R2.id.tv_avatar)
    TextView  mTvAvatar;
    @BindView(R2.id.tv_remark)
    TextView  mTvRemark;
    @BindView(R2.id.tv_desc)
    TextView  mTvDesc;

    //我发 未接
    @BindView(R2.id.tv_hint)
    NoPaddingTextView mTvHint;
    @BindView(R2.id.tv_serial_number)
    NoPaddingTextView mTvSerialNumber;

    //我发 已接
    @BindView(R2.id.view_receiver)
    View               mViewReceiver;
    @BindView(R2.id.iv_avatar_receiver)
    ImageView          mIvAvatarReceiver;
    @BindView(R2.id.rc_avatar_receiver)
    RCConstraintLayout mRcAvatarReceiver;
    @BindView(R2.id.tv_receiver)
    NoPaddingTextView  mTvReceiver;
    @BindView(R2.id.tv_receiver_money)
    TextView           mTvReceiverMoney;
    @BindView(R2.id.tv_receiver_time)
    TextView           mTvReceiverTime;
    @BindView(R2.id.gp_receiver)
    Group              mGpReceiver;

    //他发
    @BindView(R2.id.tv_other_money)
    TextView mTvOtherMoney;
    @BindView(R2.id.gp_other_send)
    Group    mGpOtherSend;

    public static void start(Activity act, WechatContactBean sendWechatContactBean, WechatContactBean receiveWechatContactBean, WechatMsgBean wechatMsgBean, String orderNum) {
        Intent it = new Intent(act, AliPayRedPacketDetailActivity.class);
        it.putExtra("orderNum", orderNum);
        it.putExtra("wechatMsgBean", wechatMsgBean);
        it.putExtra("sendContactBean", sendWechatContactBean);
        it.putExtra("receiveContactBean", receiveWechatContactBean);
        act.startActivity(it);
    }

    public AliPayRedPacketDetailActivity() {
        super(R.layout.activity_alipay_red_packet_detail);
        isChangeStatusBarTxtColor = false;
    }

    @SuppressLint({"SimpleDateFormat", "SetTextI18n"})
    @Override
    protected void initView() {
        setStatusBarColor(Color.parseColor("#8e292a"));

        WechatMsgBean wechatMsgBean = (WechatMsgBean) getIntent().getSerializableExtra("wechatMsgBean");
        if (wechatMsgBean == null)
            return;
        //通用
        WechatContactBean sendContactBean = (WechatContactBean) getIntent().getSerializableExtra("sendContactBean");
        //头像,名称,备注
        WechatContactBean.setupAvatarIntoImageView(sendContactBean, mIvAvatar, 12);
        mTvAvatar.setText(getNameWithMe(sendContactBean.getName()));
        mTvRemark.setText(wechatMsgBean.getMoneyRemark());
        //红包编号
        String orderNum = getIntent().getStringExtra("orderNum");
        mTvSerialNumber.setText("红包编号：" + orderNum);
        //金额
        String format = MoneyFormat.formatMoney(wechatMsgBean.getMoney());
        //选择布局
        if (wechatMsgBean.getMoneySenderID() == -1) {//我发的红包
            mGpOtherSend.setVisibility(View.GONE);
            if (wechatMsgBean.isReceiveMoney()) {//已领取
                mTvHint.setVisibility(View.GONE);
                mGpReceiver.setVisibility(View.VISIBLE);
                mTvDesc.setText("1人已领取，共" + format + "元");
                //接收信息
                WechatContactBean receiveContactBean = (WechatContactBean) getIntent().getSerializableExtra("receiveContactBean");
                WechatContactBean.setupAvatarIntoImageView(receiveContactBean, mIvAvatarReceiver, 12);
                mTvReceiver.setText(getNameWithMe(receiveContactBean.getName()));
                mTvReceiverMoney.setText(format + "元");
                mTvReceiverTime.setText(WechatDateFormat.formatRedPacketTime(wechatMsgBean.getReceiveTime(), new SimpleDateFormat("MM月dd日 HH:mm")));
            } else {//未领取
                mTvDesc.setText("红包金额" + format + "元，等待对方领取");
            }
        } else {
            mTvHint.setVisibility(View.GONE);
            mTvDesc.setVisibility(View.GONE);
            mGpReceiver.setVisibility(View.GONE);
            mGpOtherSend.setVisibility(View.VISIBLE);
            SpannableString spannableString = new SpannableString(format + "元");
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#d13a3c")), 0, spannableString.length() - 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(4.2f), 0, spannableString.length() - 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            mTvOtherMoney.setText(spannableString);
        }
    }

    @OnClick(R2.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
